package com.coder4j.auth.service;

import com.coder4j.auth.entity.AuthApiEntity;
import com.coder4j.auth.mapper.AuthApiMapper;
import com.coder4j.auth.service.AuthRefService;
import com.coder4j.auth.vo.AuthApiQuery;
import com.coder4j.auth.vo.AuthApiVO;
import com.coder4j.auth.service.AuthApiService;
import com.coder4j.cache.RoleCache;
import com.coder4j.starter.tools.enums.SeparatorEnums;
import com.coder4j.starter.tools.util.ListUtils;
import com.coder4j.starter.web.BaseServiceImpl;
import com.coder4j.starter.web.GroupOptionVO;
import com.coder4j.starter.web.OptionVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 接口
 * @author keller
 * @date 2024-05-07 18:10:10
 */
@Service
@Slf4j
public class AuthApiService extends BaseServiceImpl<AuthApiMapper,AuthApiEntity,AuthApiVO> {

    @Resource
    private AuthApiMapper mapper;

    @Value("${spring.application.name}")
    private String appName;



    @Override
    public AuthApiVO entity2Vo(AuthApiEntity entity) {
        AuthApiVO vo = new AuthApiVO();
        BeanUtils.copyProperties(entity,vo);
        vo.setUseDataScopeStr(entity.getUseDataScope().equals(0) ? "否" : "是");
        return vo;
    }

    @Override
    public AuthApiEntity vo2Entity(AuthApiVO vo) {
        AuthApiEntity entity = new AuthApiEntity();
        BeanUtils.copyProperties(vo, entity);
        return entity;
    }

    public void batchSave(List<AuthApiEntity> apiList) {
        String appName = apiList.get(0).getAppName();
        mapper.deleteByAppName(appName);
        mapper.baseInsertList(apiList);
    }

    public List<GroupOptionVO> getGroupOptions() {
        //查询所有API
        List<AuthApiEntity> list = this.baseQueryList(new AuthApiQuery());

        List<GroupOptionVO> voList = new ArrayList<>();
        if(ListUtils.isEmpty(list)){
            return voList;
        }

        //按照ControllerName分组
        Map<String, List<AuthApiEntity>> map = list.stream().collect(Collectors.groupingBy(AuthApiEntity::getControllerName));

        //遍历分组
        for (String key : map.keySet()) {
            GroupOptionVO vo = new GroupOptionVO();
            vo.setLabel(key);

            List<AuthApiEntity> subList = map.get(key);
            List<OptionVO> options = new ArrayList<>();
            for (AuthApiEntity api : subList) {
                options.add(new OptionVO(api.getName(), api.getSignatureCode()));
            }

            vo.setOptions(options);

            voList.add(vo);
        }

        return voList;
    }

    public boolean checkApiRights(Long roleId, HttpServletRequest request) {
        String signatureCode = request.getMethod() + SeparatorEnums.Colon.value + request.getRequestURI();

        log.info("checkApiRights roleId:{},signatureCode:{}",roleId,signatureCode);
        List<String> apis = RoleCache.getApis(roleId);
        if(ListUtils.notEmpty(apis) && apis.contains(signatureCode)){
            return true;
        }
        return false;
//        return true;
    }
}
